import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: jzj
 * Date: 2024-03-25
 * Time: 23:15
 */
public class test_2 {
    public static int uniquePathsWithObstacles(int[][] obstacleGrid) {
        // 1. 创建 dp 表
        if(obstacleGrid[0][0]==1){
            return 0;
        }
        int n=obstacleGrid.length;
        int m=obstacleGrid[0].length;
        int[][] dp = new int[n][m];
        // 2. 初始化
        for (int i = 0; i < n; i++) {
            if (obstacleGrid[i][0]==1){
                dp[i][0]=0;
                break;
            }
                dp[i][0] = 1;
        }
        for (int i = 0; i < m; i++) {
            if (obstacleGrid[0][i]==1){
                dp[0][i]=0;
                break;
            }
                dp[0][i] = 1;
        }
        // 3. 填表
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < m; j++) {
                if (obstacleGrid[i][j]==1){
                    dp[i][j]=0;
                    continue;
                }
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        for (int i = 0; i < n; i++) {
            System.out.println(Arrays.toString(dp[i]));
        }
        // 4. 返回值
        return dp[n-1][m-1];

    }

    public static void main(String[] args) {
        int[][] ost ={{0,0,0},{0,1,0},{0,0,0}};
        int[][] ost1 ={{0,1},{0,0}};
        System.out.println(uniquePathsWithObstacles(ost));
        System.out.println(uniquePathsWithObstacles(ost1));
    }
}
